package cn.dt70.controller;

import cn.dt70.pojo.AqbProduct;
import cn.dt70.pojo.AqbProject;
import cn.dt70.service.AqbProductService;
import cn.dt70.service.AqbProjectService;
import cn.dt70.utils.Constants;
import cn.dt70.utils.GetCode;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/loan_project/")
public class ApplyLoanController {

    @Resource
    AqbProjectService aqbProjectService;

    @Resource
    AqbProductService aqbProductService;

    @RequestMapping("toApply")
    public String toApply() {

        return "pre/applyLoanBase";
    }

    @RequestMapping("addBase")
    public String addBase(AqbProject aqbProject, Model model) {

        AqbProject newPro = new AqbProject();
        newPro = aqbProject;

        if (newPro.getProType() == 1) {
            newPro.setProName("爱车贷-" + newPro.getProName());
            newPro.setProCode("XACD" + GetCode.getSerialNumber(9));
        } else if (newPro.getProType() == 2) {
            newPro.setProName("爱房贷-" + newPro.getProName());
            newPro.setProCode("XAFD" + GetCode.getSerialNumber(9));
        } else if (newPro.getProType() == 3) {
            newPro.setProName("消费金融-《" + newPro.getProName() + "》");
            newPro.setProCode("XXFD" + GetCode.getSerialNumber(9));
        }
        newPro.setCreaterId(2);
        newPro.setProAmount(newPro.getProAmount() * 10000);
        newPro.setRate(newPro.getRate().divide(new BigDecimal("100.0")));

        if (aqbProjectService.addProBase(newPro)) {
            AqbProject project = aqbProjectService.findOneProject(null, newPro.getProCode());
            model.addAttribute("project", project);
        }

        return "forward:toApplyDetail";
    }

    @RequestMapping(value = "projectList", produces = "application/json; charset=utf-8")
    @ResponseBody()
    public String projectList(
            @RequestParam(required = false) Integer createrId,
            @RequestParam(required = false) Integer proType,
            @RequestParam(required = false) Integer proStatus,
            @RequestParam(required = false) Integer pageIndex
    ) {

        List<AqbProject> aqbProjectList = aqbProjectService.findProjectList(createrId, proType, proStatus, null, pageIndex);

        return JSON.toJSONString(aqbProjectList);
    }

    @RequestMapping(value = "getProjectById", produces = "application/json; charset=utf-8")
    @ResponseBody()
    public String getProjectById(Integer proId,String proCode){

        AqbProject aqbProject = aqbProjectService.findOneProject(proId,proCode);

        return JSON.toJSONString(aqbProject);
    }

    @RequestMapping(value = "newBid",produces = "application/json; charset=utf-8")
    @ResponseBody
    public String newBid(@RequestParam(required = false) Integer proId,
                         @RequestParam(required = false) Integer newBid){
        String backStr = "";
        if (aqbProjectService.modifyCurrentBids(newBid,proId)){
            AqbProject aqbProject = aqbProjectService.findOneProject(proId,null);
            backStr = JSON.toJSONString(aqbProject);
        }

        return backStr;
    }

    //------------------ 项目详情 -------------------

    @RequestMapping(value = "projectDetail", produces = "application/json; charset=utf-8")
    @ResponseBody
    public String projectDetail(@RequestParam(required = false) Integer pjId,
                                @RequestParam(required = false) String proCode) {

        String backDetail = JSON.toJSONString(aqbProductService.findOneProjectDetail(pjId, proCode));

        return backDetail;
    }

    @RequestMapping("toApplyDetail")
    public String toApplyDetail() {

        return "pre/applyLoanDetail";

    }

    @RequestMapping("addDetail")
    public String addDetail(AqbProduct aqbProduct,
                            @RequestParam(required = false) String pawnStatus,
                            @RequestParam(required = false) String certificateStatus,
                            @RequestParam(required = false) Integer loanAmount,
                            @RequestParam(required = false, value = "pics") MultipartFile[] pics,
                            Model model
    ) {
        Integer projectId = null;
        AqbProject aqbProject = null;

        if (null != aqbProduct) {
            projectId = aqbProduct.getPjId();

        }
        String summary = "借款金额为" + loanAmount + "万元。借款人的信用良好。"+"证件状况："+certificateStatus+"  实物状况："+pawnStatus;
        aqbProduct.setpSummary(summary);

        if (null != pics && pics.length > 0) {
            for (int i = 0; i < pics.length; i++) {
                MultipartFile multipartFile = pics[i];
                if (!multipartFile.isEmpty()) {
                    try {

                        File dir = new File(Constants.logoPicPath);
                        if (!dir.exists()) {
                            dir.mkdirs();
                        }

                        String saveDicPath = Constants.logoPicPath + multipartFile.getOriginalFilename();
                        multipartFile.transferTo(new File(saveDicPath));

                        String srcPath = Constants.logoLocPath + multipartFile.getOriginalFilename();
                        if (i == 0) {
                            aqbProduct.setTopicPath(srcPath);
                        } else if (i == 1) {
                            aqbProduct.setOtherPic1(srcPath);
                        } else if (i == 2) {
                            aqbProduct.setOtherPic2(srcPath);
                        } else if (i == 3) {
                            aqbProduct.setOtherPic3(srcPath);
                        } else if (i == 4) {
                            aqbProduct.setOtherPic4(srcPath);
                        } else if (i == 5) {
                            aqbProduct.setOtherPic5(srcPath);
                        } else if (i == 6) {
                            aqbProduct.setOtherPic6(srcPath);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
        }

        if (aqbProductService.addOneProjectDetail(aqbProduct)) {
            AqbProduct newInsertProDetail = aqbProductService.findOneProjectDetail(projectId,null);
            if (aqbProjectService.modifyDetailIdOfProject(newInsertProDetail.getpId(),projectId)){
                aqbProject=aqbProjectService.findOneProject(projectId,null);
                model.addAttribute("aqbProject",aqbProject);
                model.addAttribute("aqbProduct",newInsertProDetail);
            }
            return "pre/applyLoanOk";
        } else {
            return "forward:toApplyDetail";
        }

    }


}
